<template>
  <div class="search">
    <el-input v-model="projectName" style="max-width: 500px" placeholder="搜索公开项目" clearable @clear="searcClear">
      <template #append>
        <el-button :icon="Search" @click="searchProject" />
      </template>
    </el-input>
  </div>
  <div class="main">
    <div class="menu">
      <el-menu :default-active="defaultActive">
        <el-menu-item v-for="(item, index) in menuList" :key="index" :index="item.tagId" @click="clickMenu(item)">
          <span>{{ item.tagName }}</span>
        </el-menu-item>
      </el-menu>
    </div>
    <div class="content">
      <div class="content-title">{{ contentTitle }}</div>
      <div class="content-card">
        <div class="card" v-for="(item, index) in cardList" :key="index" @click="toApidoc(item)">
          <div class="card-border">
            <div class="card-header">
              <div class="avatat">
                <img :src="item.logo" alt="" style=" width: 40px;height: 40px;" />
              </div>
              <div class="title">{{ item.name }}</div>
            </div>
            <div class="card-des">{{ item.description }}
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { Search } from '@element-plus/icons-vue'
import * as apihubApi from '@/api/apihub'
const { push } = useRouter()
const defaultActive = ref(0)
const menuList = ref([])
const cardList = ref([])
const contentTitle = ref('全部分类')
const clickMenu = async (item) => {
  contentTitle.value = item.tagName
  cardList.value = await apihubApi.getProjectByTagId({ tagId: item.tagId })
}
const projectName = ref('')
const searchProject = async () => {
  cardList.value = await apihubApi.getProjectByName({ name: projectName.value })
}

const searcClear = () => {
  searchProject()
}

import { useCache } from '@/hooks/web/useCache'
const { wsCache } = useCache()
const toApidoc = (item) => {
  push({ name: 'Apidoc', state: { ...item } })
  wsCache.set('enviromentParams', item)
}
onMounted(async () => {
  menuList.value = await apihubApi.getTagList()
  cardList.value = await apihubApi.getProjectByTagId({ tagId: 0 })
});
</script>
<style scoped lang="scss">
.search {
  display: flex;
  justify-content: center;
  align-items: center;
  padding-top: .75rem;
  margin-top: 1.5rem;
  margin-bottom: 1.5rem;
}

.main {
  flex-direction: row;
  min-height: auto;
  box-sizing: border-box;
  display: flex;
  flex: auto;
  min-height: 0;
  background: #fff;

  .menu {
    flex: 0 0 200px;
    max-width: 200px;
    min-width: 200px;
    width: 200px;
    height: calc(100vh - 170px);
    margin-right: 10px;
    padding-right: 10px;
    padding-bottom: 10px;
    overflow-y: auto;
    background-color: transparent;
    position: relative;
    min-width: 0;
    background: #fff;
    transition: all .2s;
  }

  .content {
    height: calc(100vh - 170px);
    overflow-y: auto;
    padding-right: 1.25rem;
    flex: auto;
    min-height: 0;

    .content-title {
      display: flex;
      justify-content: space-between;
      margin-bottom: .5rem;
    }

    .content-card {
      display: flex;
      flex-flow: row wrap;
      min-width: 0;

      .card {
        width: 30%;
        display: block;
        max-width: 100%;
        margin-bottom: 16px;
        padding-top: 0;
        padding-bottom: 0;
        border-bottom: none;
        background-color: transparent;
        padding: .5rem;

        .card-border {
          padding: 0.5rem;
          border: 1px solid #f3f5f6;
          border-radius: 8px;
        }

        .card-border:hover {
          border-color: rgba(147, 115, 238, 0.45);
          cursor: pointer;
        }

        .card-header {
          display: flex;
          flex-flow: row wrap;
          min-width: 0;
          align-items: center;
          flex-wrap: nowrap;
          position: relative;

          .avatat {
            line-height: 40px;
            font-size: 18px;
            border: 1px solid rgba(86, 87, 88, 0.04);
            display: block;
          }

          .title {
            width: 80%;
            margin-left: .5rem;
            max-height: 2.5rem;
            font-size: .875rem;
          }
        }

        .card-des {
          display: -webkit-box;
          white-space: normal;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
          text-overflow: ellipsis;
          margin-bottom: 1rem;
          margin-top: .5rem;
          line-height: 1.25rem;
          font-size: .75rem;
          height: 2.5rem;
        }
      }
    }
  }
}
</style>
<style scoped>
.el-menu {
  border-right: 0px;
  margin: 0 10px;
}

.el-menu-item {
  height: 35px;
  line-height: 35px;
  padding: 0 10px;
  border-radius: 6px;
}

.el-menu-item:hover {
  background-color: #f2f4f7;
}

.el-menu-item.is-active {
  background-color: #f2f4f7;
  color: #000;
}
</style>